package org.luxor.common.netty.config;

import org.luxor.common.netty.TcpNioClient;
import org.luxor.common.netty.config.properties.NettyClientProperties;
import org.luxor.common.netty.protocol.NettyClientPipelineConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Netty 客户端全局配置
 *
 * @author Mr.Yan  @date: 2022/04/06
 */
@Configuration
@EnableConfigurationProperties(NettyClientProperties.class)
@ConditionalOnProperty(prefix = NettyClientProperties.PREFIX, name = "enabled", havingValue = "true")
public class NettyClientAutoConfig {

    @Bean
    @ConditionalOnMissingBean(TcpNioClient.class)
    public TcpNioClient tcpNioClient(NettyClientProperties nettyClientProperties) {
        // 启动服务
        TcpNioClient tcpNioClient = new TcpNioClient(new NettyClientPipelineConfig(nettyClientProperties));
        tcpNioClient.start();
        return tcpNioClient;
    }

}
